<?php
// +----------------------------------------------------------------------
// | JSHOP [ 小程序商城 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://jihainet.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tianyu <tianyu@jihainet.com>
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Validate;

class Link extends Common
{
    protected $autoWriteTimestamp = true;
    protected $createTime = 'ctime';
    protected $updateTime = 'utime';

    protected $rule = [
        'name' => 'require|max: 100',
        'url' => 'require|url',
        'sort' => 'require|number|integer|max:1000'
    ];

    protected $msg = [
        'name.require' => '请输入链接名称',
        'name.max' => '链接名称长度超出限制',
        'url.require' => '请输入链接地址URL',
        'url.url' => '不是合法的链接地址URL',
        'sort.require' => '请输入排序',
        'sort.number' => '排序只能是数字',
        'sort.integer' => '排序是能是整数',
        'sort.max' => '排序大小超出限制'
    ];

    public function tableData($post)
    {
        return parent::tableData($post); // TODO: Change the autogenerated stub
    }


    public function checkData ($data = [])
    {
        $result = [
            'status' => true,
            'msg' => '验证通过',
            'data' => []
        ];

        $validate = new Validate($this->rule, $this->msg);
        if (!$validate->check($data)){
            $result['status'] = false;
            $result['msg'] = $validate->getError();
        }

        return $result;
    }

    public function addData ($data = [])
    {
        $result = [
            'status' => true,
            'msg' => '保存成功',
            'data' => []
        ];


        $checkStatus = $this->checkData($data);
        if ($checkStatus['status']) {
            if (!$this->allowField(true)->save($data))
            {
                $result['status'] = false;
                $result['msg'] = '保存失败';
            }
        } else {
            $result['status'] = false;
            $result['msg'] = $checkStatus['msg'];
        }

        return $result;
    }


    public function editData ($data = [])
    {
        $result = [
            'status' => true,
            'msg' => '保存成功',
            'data' => []
        ];

        $checkStatus = $this->checkData($data);

        if ($checkStatus['status']) {
            if (!$this->allowField(true)->save($data, ['id' => $data['id']])) {
                $result['status'] = false;
                $result['msg'] = '保存失败';
            }
        } else {
            $result['status'] = false;
            $result['msg'] = $checkStatus['msg'];
        }

        return $result;

    }


    public function getInfo ($id = 0)
    {
        $result = [
            'status' => true,
            'msg' => '获取成功',
            'data' => []
        ];

        if (!$id) {
            $result['status'] = false;
            $result['msg'] = '缺少参数id';
            return  $result;
        }

        $field = 'id,name,image,url,ctime,utime,sort';

        $info = $this->field($field)->where('id', $id)->find();


        if (!$info) {
            $result['status'] = false;
            $result['msg'] = '未找到该条记录';
            return  $result;
        }

        $result['data'] = $info->toArray();

        return $result;
    }


    protected function tableWhere($post)
    {
        $where = [];
        if (isset($post['name']) && $post['name'] !== '')
        {
            $where[] = ['name', 'like', '%'.$post['name'].'%'];
        }
        if (isset($post['date']) && $post['date'] !== '')
        {
            $dateArr = explode('~', $post['date']);
            $_stime = strtotime($dateArr[0].'00:00:00', time());
            $_utime = strtotime($dateArr[1].'23:59:59', time());
            $where[] = ['utime', ['EGT', $_stime], ['ELT', $_utime], 'and'];
        }
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = ['sort asc'];
        return $result;
    }



    protected function tableFormat($list)
    {
        foreach ($list as $key => $val) {
            $list[$key]['ctime'] = getTime($val['ctime']);
            $list[$key]['utime'] = getTime($val['utime']);
            $list[$key]['image'] = _sImage($val['image']);
        }
        return $list;
    }





    /**
     *
     *  获取友情链接列表
     *
     */
    public function getList ($page = 1, $limit = 10, $field = 'id,image,name,url')
    {
        $result = [
            'status' => true,
            'msg' => '获取成功',
            'data' => []
        ];

        $list = $this->field($field)->page($page, $limit)->select();

        $count = $this->count();
        if (!$list->isEmpty())
        {
            foreach ($list as &$val)
            {
                $val['image'] = _sImage($val['image']);
            }

            $result['data'] = $list;
        }

        return  $result;
    }

}